package org.xshop.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.xshop.mapper.TExpressmanAccountMapper;
import org.xshop.model.TExpressmanAccount;
import org.xshop.model.TOrder;
import org.xshop.model.TOrderFee;
import org.xshop.service.ITExpressmanAccountService;

import com.weibo.api.motan.config.springsupport.annotation.MotanService;

import top.ibase4j.core.base.BaseServiceImpl;
import top.ibase4j.core.util.DateUtil;

/**
 * <p>
 * 骑手交易账户 服务实现类
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-05-30
 */
@MotanService(interfaceClass = ITExpressmanAccountService.class)
@CacheConfig(cacheNames = "TExpressmanAccount")
public class ExpressmanAccountService extends BaseServiceImpl<TExpressmanAccount, TExpressmanAccountMapper>
implements ITExpressmanAccountService {
    @Autowired
    private OrderService orderService;

    @Override
    public Object pay(TExpressmanAccount param) {
        String ip = param.getOpenid();
        param.setOpenid(null);
        TExpressmanAccount account = selectOne(param);
        TOrder record = new TOrder();
        record.setMemberId(account.getId());
        String orderNo = "T" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + record.getMemberId()
        + RandomUtils.nextInt(100, 999);
        record.setOrderNo(orderNo);
        record.setOrderTime(new Date());
        record.setSkuType("0");
        TOrderFee fee = new TOrderFee();
        fee.setPayAmount(new BigDecimal("0.01"));
        record.setFee(fee);
        record = orderService.update(record);
        return orderService.createPayOrder(record.getId(), param.getAccountType(), ip);
    }
}
